package com.xrs.xrseducation.course.service.impl;

import com.xrs.xrseducation.course.entity.Course;
import com.xrs.xrseducation.course.entity.CourseExt;
import com.xrs.xrseducation.course.mapper.CourseMapper;
import com.xrs.xrseducation.course.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xrs.xrseducation.outline.entity.Outline;
import com.xrs.xrseducation.outline.mapper.OutlineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Logger;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-09-18
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    OutlineMapper outlineMapper;

    /**
     * 添加课程进入数据库,并添加课程大纲数据
     */
    @Override
    public void addCourse(CourseExt courseExt){
        System.out.println("添加操作"+courseExt.toString());

        //添加课程
        courseMapper.insert(courseExt);
        //获取课程id
        Integer courseId = courseExt.getCourseId();
        //获取总讲数的集合
        ArrayList<Outline> list=courseExt.getOutlines();
        //获取当前课程的总讲数
        int allNumber =list.size();
        for (int i = 0; i < allNumber; i++) {
            Outline outline = list.get(i);
            outline.setCourseId(courseId);
            System.out.println("每一讲的详细内容"+outline);
            outlineMapper.insert(outline);
        }
    }

    /**
     * 删除课程
     */
    public void deleteCourse(Course course){
        System.out.println("删除操作"+course.toString());
        courseMapper.deleteById(course.getCourseId());
        //删除课程表
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("courseId",course.getCourseId());
        //删除课程大纲表
        outlineMapper.deleteByMap(hashMap);
    }

    /**
     * 修改课程
     */
    public void updateCourse(CourseExt courseExt){
        System.out.println("课程修改"+courseExt.toString());
        courseMapper.updateById(courseExt);

    }

    /**
     * 根据id查询课程
     */
    public void queryById(Integer courseId){
        courseMapper.selectById(courseId);
    }

    /**
     * 查询全部课程
     */
    public void queryAll(){
        courseMapper.selectList(null);
    }
}
